package models

import "go-project/common"

type Tag struct {
	ID        uint              `gorm:"primary_key" json:"id"`
	CreatedAt *common.LocalTime `json:"created_at"`
	UpdatedAt *common.LocalTime `json:"updated_at"`
	DeletedAt *common.LocalTime `sql:"index" json:"deleted_at"`
	Name      string            `gorm:"type:varchar(255);not null;comment:'标签名称'" json:"name"`
	Weight    int8              `gorm:"not null;default:0;comment:'标签权重'" json:"weight"`
	Color     string            `json:"color"`
}

func (tags *Tag) TableName() string {
	return "tags"
}

func (tag *Tag) CheckNameExist(name string, id uint) bool {
	query := common.DB.Where("name=?", name)
	if id != 0 {
		query = query.Not("id", id)
	}
	if query.First(tag).RecordNotFound() {
		return false
	}
	return true
}
